/**
 * @author xiatao
 * 2015年9月9
 */
(function($){
	var ms = {
		init:function(obj,args){
			return (function(){
				ms.fillHtml(obj,args);
				ms.bindEvent(obj,args);
			})();
		},
		fillHtml:function(obj,args){
			return (function(){
				//obj.empty();
				var selected10='';
				if(args.pageCount==10){
					selected10='selected="selected"';
				}
				var selected20='';
				if(args.pageCount==20){
					selected20='selected="selected"';
				}
				var selected30='';
				if(args.pageCount==30){
					selected30='selected="selected"';
				}
				var selected40='';
				if(args.pageCount==40){
					selected40='selected="selected"';
				}
				var selected50='';
				if(args.pageCount==50){
					selected50='selected="selected"';
				}
				var pageNumHtml='';
				var current=parseInt(args.current);
				var totalSize=parseInt(args.totalSize);
				var pageCount=parseInt(args.pageCount);
				var totalPages=(totalSize % pageCount == 0 ? totalSize / pageCount
					: Math.ceil(totalSize / pageCount));
				if(totalPages==0){
					pageNumHtml='<a href="javascript:;" class="current" style="display: none">'+0+'</a>';
				}else if(totalPages<=9){
					for(var i=1;i<=totalPages;i++){
						if(current==i){
							pageNumHtml+='<a href="javascript:;" class="current">'+i+'</a>';
						}else{
							pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+i+'</a>';
						}
					}
				}else{
					if(current<=9){
						pageNumHtml='';
						for(var i=1;i<=9;i++){
							if(current==i){
								pageNumHtml+='<a href="javascript:;" class="current">'+i+'</a>';
							}else{
								pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+i+'</a>';
							}
						}
						pageNumHtml+='<a href="javascript:;">…</a>';
						pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+totalPages+'</a>';
					}else if((current+9)>=totalPages){
						pageNumHtml='';
						pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+1+'</a>';
						pageNumHtml+='<a href="javascript:;">…</a>';
						for(var j=(totalPages-10);j<totalPages;j++){
							if(current==(j+1)){
								pageNumHtml+='<a href="javascript:;" class="current">'+(j+1)+'</a>';
							}else{
								pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+(j+1)+'</a>';
							}
						}
					}else{
						pageNumHtml='';
						pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+1+'</a>';
						pageNumHtml+='<a href="javascript:;">…</a>';
						var kStart=parseInt(current-5);
						var kEnd=parseInt(current+5);
						for(var k=kStart;k<kEnd;k++){
							if(current==k){
								pageNumHtml+='<a href="javascript:;" class="current">'+k+'</a>';
							}else{
								pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+k+'</a>';
							}
						}
						pageNumHtml+='<a href="javascript:;">…</a>';
						pageNumHtml+='<a href="javascript:;" class="tcdNumber">'+totalPages+'</a>';
					}


				}

				var html='';
				html+='<div class="total-p"> <span class="xs1">每页显示</span><select style="width:50px;float:left;margin-top:13px;" id="pageSelect"><option value="10"'+
					selected10+
					'>10</option><option value="20"'+
					selected20+
					'>20</option><option value="30"'+
					selected30+
					'>30</option><option value="40"'+
					selected40+
					'>40</option><option value="50"'+
					selected50+
					'>50</option></select><span class="xuanx">条，共<i id="totalSize">'+
					args.totalSize+
					'</i>条</span></div><p class="paging0 clearFix"><a href="javascript:;" class="a01"> < </a>' +
					pageNumHtml+
					'<a href="javascript:;" class="a02" id="prevPage"> > </a><span>跳转到</span><input type="text" onkeyup="valInput('+args.current+');" id="jump" value="'+
					args.current
					+'"/><span>&nbsp;页</span> <button id="jumpBtn">确 定</button> </p></div>';
				obj.html(html);
			})();
		},

		bindEvent:function(obj,args){
			return (function(){
				obj.on("change","select",function(){
					var current=args.current;
					var pageCount = parseInt($(this).val());
					var totalSize=args.totalSize;
					ms.fillHtml(obj,{"current":1,"pageCount":pageCount,totalSize:totalSize});
					var url=args.url;
					var fillDiv=args.fillDiv;
					if(typeof(args.backFn)=="function"){
						args.backFn(1,pageCount,totalSize,url,fillDiv);
					}
				});
				obj.on("click","a.a01",function(){
					var current = parseInt(obj.find("a.current").html());
					var pageCount = parseInt($("#pageSelect").val());
					var totalSize=parseInt(args.totalSize);
					var url=args.url;
					var fillDiv=args.fillDiv;
					if(current<=1){
						alert("已经是第一页");
					}else{
						ms.fillHtml(obj,{"current":current-1,"pageCount":pageCount,totalSize:totalSize});
						if(typeof(args.backFn)=="function"){
							args.backFn(current-1,pageCount,totalSize,url,fillDiv);
						}
					}
				});
				obj.on("click","a#prevPage",function(){
					var current = parseInt(obj.find("a.current").html());
					var pageCount = parseInt($("#pageSelect").val());
					var totalSize=parseInt(args.totalSize);
					var totalPages=(totalSize % pageCount == 0 ? totalSize / pageCount
						: Math.ceil(totalSize / pageCount));
					var url=args.url;
					var fillDiv=args.fillDiv;
					if(current>=totalPages){
						alert("已经是最后一页");
					}else{
						ms.fillHtml(obj,{"current":current+1,"pageCount":pageCount,totalSize:totalSize});
						if(typeof(args.backFn)=="function"){
							args.backFn(current+1,pageCount,totalSize,url,fillDiv);
						}
					}
				});
				 obj.on("click","a.tcdNumber",function(){
				 var current = parseInt($(this).text());
				 var pageCount = parseInt($("#pageSelect").val());
				 var totalSize=args.totalSize;
				 var url=args.url;
				 var fillDiv=args.fillDiv;
				 ms.fillHtml(obj,{"current":current,"pageCount":pageCount,totalSize:totalSize});
				 if(typeof(args.backFn)=="function"){
				 args.backFn(current,pageCount,totalSize,url,fillDiv);
				 }
				 });
				obj.on("click","#jumpBtn",function(){
					var current = $("#jump").val();
					var pageCount = parseInt($("#pageSelect").val());
					var totalSize=args.totalSize;
					var totalPages=(totalSize % pageCount == 0 ? totalSize / pageCount
						: Math.ceil(totalSize / pageCount));
					var url=args.url;
					var fillDiv=args.fillDiv;
					if(isNaN(current)||(parseInt(current)<=0)||(parseInt(current)>totalPages)){
						//alert("请输入大于0小于"+totalPages+"的数字");
						alert("无效的跳转页数，请重新输入。");
					   return;
					}
					ms.fillHtml(obj,{"current":current,"pageCount":pageCount,totalSize:totalSize});
					if(typeof(args.backFn)=="function"){
						args.backFn(current,pageCount,totalSize,url,fillDiv);
					}
				});
			})();
		}
	}
	$.fn.createPage = function(options){
		var args = $.extend({
			current :1,
			pageCount:10,
			totalSize:0,
			url:'',
			fillDiv:'',
			backFn : function(){

			}
		},options);
		ms.init(this,args);
	}
})(jQuery);

/**
 * 2015-11-12
 * 跳转页数验证
 */
function valInput(a){
	var inpVal = $("#jump").val();
	if (!isNaN(inpVal) && inpVal > 0) {
//		alert(inpVal)
	} else {
		$("#jump").val(a);
		alert('请输入正整数');
	}
}